//
//  ScenesSettingVCModel.m
//  Yihubang
//
//  Created by hyu on 2016/11/30.
//  Copyright © 2016年 xmy. All rights reserved.
//

#import "ScenesSettingVCModel.h"
#import "ScenesTableViewCell.h"
#import "ScenesHeaderView.h"
#import "PlaySoundView.h"
#import "PlayAudioView.h"
@interface ScenesSettingVCModel()<PlaySoundDelegate,PlayAudioViewDelegate>
@property (nonatomic,strong) ScenesHeaderView *scenesHeaderView;
@property (nonatomic,strong) PlaySoundView *playSoundView;
@property (nonatomic,strong) PlayAudioView *playAudioView;
@property (nonatomic,strong) NSMutableArray *infoDataSource;
@property (nonatomic,strong) NSArray *sectionTitleArr;
@property (nonatomic,strong) NSIndexPath *lastIndexPath;
@property (nonatomic,strong) NSIndexPath *cIndexPath;
@property (nonatomic,assign) NSInteger lastTag;
@property (nonatomic,assign) NSInteger cTag;
@property (nonatomic,strong) AVAudioPlayer *playMusic;
@end
@implementation ScenesSettingVCModel
- (void)initialize
{
    [super initialize];
    self.cTag = 1000;
    @weakify(self)
    [[[RACObserve(self, dataSource)
       filter:^(NSArray *events) {
           return @(events.count > 0).boolValue;
       }]
      deliverOnMainThread]
     subscribeNext:^(NSArray *events) {
         @strongify(self)
         [self.mTableView reloadData];
     }];
    self.stopCommend = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        [self removePlayAudioView];
        self.playAudioView = nil;
        return [RACSignal empty];
    }];

}

- (void)bindTableView:(UITableView *)tableView
{
    [super bindTableView:tableView];
    self.cellReuseIdentifier = @"ScenesTableViewCell";
    self.mTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.mTableView registerCellWithReuseIdentifier:@"ScenesTableViewCell"];
}

-(NSArray *)sectionTitleArr{
    if (!_sectionTitleArr) {
        _sectionTitleArr = [NSArray arrayWithObjects:@"",@"通用场景",@"情景模拟", nil];
    }
    return _sectionTitleArr;
}

-(PlaySoundView *)playSoundView{
    if (!_playSoundView) {
        _playSoundView = [[UINib nibWithNibName:@"PlaySoundView" bundle:nil] instantiateWithOwner:self options:nil].firstObject;
        _playSoundView.delegate = self;
    }
    return _playSoundView;
}

-(PlayAudioView *)playAudioView{
    if(!_playAudioView){
        NSArray* nibView     = [[NSBundle mainBundle] loadNibNamed:@"PlayAudioView" owner:nil options:nil];
        _playAudioView       = [nibView objectAtIndex:0];
        _playAudioView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        _playAudioView.delegate =self;
    }
    return _playAudioView;
}

-(AVAudioPlayer *)playMusic{
    if (!_playMusic) {
        NSString *audioViewType = @"";
        if ([self.playAudioView.phoneName.text isEqualToString:@"妈妈"]) {
            audioViewType = @"m4a";
        }else{
            audioViewType = @"mp3";
        }
        NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@",self.playAudioView.phoneName.text] ofType:audioViewType];
        _playMusic = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:filePath] error:nil];
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
        _playMusic.volume = 1.0;
    }
    return _playMusic;
}

-(void)imitateCallPhone:(NSString *) name{
    //跳转模拟拨号
//    ScenesTableViewCell *cell = [self.mTableView cellForRowAtIndexPath:self.cIndexPath];
//    switch (self.cTag) {
//        case 1:
//            self.playAudioView.phoneName.text = cell.scenesTille_1.text;
//            break;
//        case 2:
//            self.playAudioView.phoneName.text = cell.scenesTille_2.text;
//            break;
//        case 3:
            self.playAudioView.phoneName.text = name;
//            break;
//            
//        default:
//            break;
//    }
    [[UIApplication sharedApplication].keyWindow addSubview:self.playAudioView];
    [UIView animateWithDuration:0.6 animations:^(void){
        self.playAudioView.hidden =NO;
        self.playAudioView.alpha =1.0f;
    } completion:^(BOOL finish){
        [self.playAudioView.PhoneTime start];
    }];

//    设置语音播放为扬声器模式
    [self.playMusic play];
}

-(void)removePlayAudioView{
    if(self.cTag != 1000){
        [self.playMusic stop];
        self.playMusic = nil;
        self.cTag = 1000;
        [self.mTableView reloadData];
    }
}

- (void)configureCell:(ScenesTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object
{
    NSLog(@"%ld",indexPath.row);
    cell.scenesTille_1.text = object[0];
    cell.scenesTille_2.text = object[1];
    cell.scenesTille_3.text = object[2];
    cell.image_1.image = [UIImage imageNamed:cell.scenesTille_1.text];
    cell.image_2.image = [UIImage imageNamed:cell.scenesTille_2.text];
    cell.image_3.image = [UIImage imageNamed:cell.scenesTille_3.text];
    if ([cell.scenesTille_1.text isEqualToString:@""]) {
        cell.bgView_1.hidden = YES;
    }
    if([cell.scenesTille_2.text isEqualToString:@""]){
        cell.bgView_2.hidden = YES;
    }
    if([cell.scenesTille_3.text isEqualToString:@""]){
        cell.bgView_3.hidden = YES;
    }
    @weakify(self)
    [cell.image_1 setTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        @strongify(self)
        [self showView:gestureRecoginzer];
    }];
    [cell.image_2 setTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
//        [self showView:(UITapGestureRecognizer*)gestureRecoginzer];
        @strongify(self)
        [self showView:gestureRecoginzer];
//        self.cTag = gestureRecoginzer.view.tag;
//        ScenesTableViewCell *cell = (ScenesTableViewCell*)[[[gestureRecoginzer.view superview] superview] superview];
//        UILabel * name = [cell viewWithTag:(self.cTag +3)];
//        [self imitateCallPhone:name.text];
    }];
    [cell.image_3 setTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
//        [self showView:(UITapGestureRecognizer*)gestureRecoginzer];
        @strongify(self)
        [self showView:gestureRecoginzer];
//        [self imitateCallPhone];
    }];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
//    if ([indexPath isEqual:self.cIndexPath]) {
//        if (self.cTag == self.lastTag) {
//        }else{
//            switch (self.cTag) {
//                case 1:
//                    _cell.scenesImage_1.highlighted = YES;
//                    break;
//                case 2:
//                    _cell.scenesImage_2.highlighted = YES;
//                    break;
//                case 3:
//                    _cell.scenesImage_3.highlighted = YES;
//                    break;
//                    
//                default:
//                    break;
//            }
//        }
//    }
//    else{
//        _cell.constraintHeight.constant = 1;
//    }
    
}

-(void)showView:(UIGestureRecognizer*)gestureRecoginzer{
    self.cTag = gestureRecoginzer.view.tag;
    ScenesTableViewCell *cell = (ScenesTableViewCell*)[[[gestureRecoginzer.view superview] superview] superview];
    UILabel * name = [cell viewWithTag:(self.cTag +3)];
    [self imitateCallPhone:name.text];

//    ScenesTableViewCell *cell = (ScenesTableViewCell*)[[[tap.view superview] superview] superview];
//    cell.isSelect = YES;
//    NSIndexPath *indexPath = [self.mTableView indexPathForCell:cell];
//    self.lastTag = self.cTag;
//    self.cTag = tap.view.tag;
//    self.lastIndexPath = self.cIndexPath;
//    if (self.lastIndexPath) {
//        [self removeLastIndexPathCellView:self.lastIndexPath];
//    }
//    self.lastIndexPath = self.cIndexPath;
//    if ([indexPath isEqual:self.cIndexPath]) {
//        if (self.lastTag == tap.view.tag) {
//            self.cIndexPath = nil;
//        }
//    }else{
//        ScenesTableViewCell *cell = (ScenesTableViewCell*)[self.mTableView cellForRowAtIndexPath:self.lastIndexPath];
//        switch (self.lastTag) {
//            case 1:
//                cell.scenesImage_1.highlighted = NO;
//                break;
//            case 2:
//                cell.scenesImage_2.highlighted = NO;
//                break;
//            case 3:
//                cell.scenesImage_3.highlighted = NO;
//                break;
//                
//            default:
//                break;
//        }
//    self.cIndexPath = indexPath;
//    }
//    [self.mTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
//}

//-(void)removeLastIndexPathCellView:(NSIndexPath*)indexPath{
//    ScenesTableViewCell *cell = [self.mTableView cellForRowAtIndexPath:indexPath];
//    cell.playSoundView = nil;
}

#pragma mark -- tableViewDataSource;
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return nil;
    }else{
        UILabel *sectionLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
        sectionLable.text = self.sectionTitleArr[section];
        sectionLable.textAlignment = NSTextAlignmentCenter;
        sectionLable.textColor = [UIColor colorWithHex:0x000000];
        sectionLable.font = [UIFont systemFontOfSize:16.0];
        return sectionLable;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0;
    }else{
        return 40;
    }
}

@end
